<?php
namespace app\admin\controller;

use think\Controller;
use think\Log;
use think\Exception;

class Type extends Base
{

    /**
     * 类别列表
     * @author wcy
     * @version 2017年6月6日14:48:16
     */
    public function typeList(){
        try{
            if($this->request->isAjax()||$this->request->isPost()){
                $data = $this->deDataTablesJson();
                $draw=$data['draw'];
                $start=$data["start"];
                $length=$data["length"];
                $search="";
                if(!empty($_POST["search"])){
                    $search=$_POST["search"];
                }
                $data=model("Type")->typeList($search,$start,$length);
                if(!$data){
                    return json(["draw"=>$draw,"recordsTotal"=>0,"recordsFiltered"=>0,"data"=>[]]);
                }else{
                    return json(["draw"=>$draw,"recordsTotal"=>$data["count"],"recordsFiltered"=>$data["count"],"data"=>$data["data"]]);
                }
            }else{
                return view("typelist");
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            return false;
        }
    }
    /**
     * 添加类别
     * @author wcy
     * @version 2017年6月6日15:01:53
     */
    public function addType(){
        if($this->request->isAjax()||$this->request->isPost()){
            $type_name=$_POST["type_name"];
            if(empty($type_name)){
                return json(["code"=>-1,"msg"=>"请输入类别名称"]);
            }else{
                $data["type_name"]=$type_name;
                $verify=model("Type")->verifyTypeName($type_name);
                if(!empty($verify)){
                    return json(["code"=>-1,"msg"=>"类别名称已存在,请重新输入!"]);
                }else{
                    $res=model("Type")->addType($data);
                    if($res!==false){
                        return json(["code"=>1,"msg"=>"添加成功"]);
                    }else{
                        return json(["code"=>-1,"msg"=>"添加失败"]);
                    }
                }
            }
        }else{
            return view("addtype");
        }
    }
    /**
     * 编辑类别
     * @author wcy
     * @version 2017年6月6日16:20:58
     */
    public function editType(){
        try{
            if($this->request->isAjax()||$this->request->isPost()){
                $id=$_POST["id"];
                $type_name=$_POST["type_name"];
                if(empty($type_name)){
                    return json(["code"=>-1,"msg"=>"请输入类别名称"]);
                }else{
                    $verify=model("Type")->verifyTypeNameTwo($id,$type_name);
                    if(!empty($verify)){
                        return json(["code"=>-1,"msg"=>"类别名称已存在,请重新输入!"]);
                    }else{
                        $data["type_name"]=$type_name;
                        $res=model("Type")->editType($id,$data);
                        if($res!==false){
                            return json(["code"=>1,"msg"=>"修改成功!"]);
                        }else{
                            return json(["code"=>-1,"msg"=>"修改失败!"]);
                        }
                    }
                }
            }else{
                $id=$_GET["id"];
                $data=model("Type")->getType($id);
                $this->assign("data",$data);
                return view("edittype");
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            return view("edittype");
        }
    }
    /**
     * 删除类别
     * @author wcy
     * @version 2017年6月6日15:52:56
     */
    public function delType(){
        try{
            $id=$_POST["id"];
            if(empty($id)){
                return json(["code"=>-1,"msg"=>"系统出错，请刷新页面"]);
            }else{
                $res=model("Type")->delType($id);
                if($res!==false){
                    return json(["code"=>1,"msg"=>"删除成功"]);
                }else{
                    return json(["code"=>-1,"msg"=>"删除失败"]);
                }
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            return false;
        }
    }
}